let mysqlpool = require('../mysql')
module.exports = {
    login (username, password, callback) {
        mysqlpool.getConnection(function (err, conn) {
            if (err) {
                callback(err);
                conn.release();// 释放连接对象
                return
            }
            // let querystr = "select * from users where account='" + username + "' and pswd='" + password + "'"
            // 把前端输入的内容作为字符 用？占位 防sql注入
            let querystr = `select * from users where account=? and pswd=?`
            console.log(querystr)
            const sqlstrParam = [username, password]
            conn.query(querystr, sqlstrParam, function (err, result) {
                // console.log(result)
                callback(err, result)
                conn.release()
            })
        })
    },
    getMenu () {
        return new Promise((resolve, reject) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    reject(err)
                    conn.release()
                    return
                }
                let querystr = `select * from menu`
                conn.query(querystr, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    },
    // 用户信息 分页
    getUserList (page) {
        return new Promise((resolve, rej) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    rej(err)
                    conn.release()
                    return
                }
                // limit限制数据显示条数 start开始条数 count总共条数
                let querystr = `select users.*,power.name as type_id from users inner join power on users.type_id=power.type_id limit ?,10`
                const sqlstrParam = [(page - 1) * 10]
                conn.query(querystr, sqlstrParam, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    },
    // 获取权限
    getPower () {
        return new Promise((resolve, rej) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    rej(err)
                    conn.release()
                    return
                }
                let querystr = `select * from power`
                conn.query(querystr, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    },
    // 新增用户
    addUserInfo (obj) {
        return new Promise((resolve, rej) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    rej(err)
                    conn.release()
                    return
                }
                const { account, pswd, type_id, username } = obj
                let querystr = `insert into users(account,pswd,type_id,username) values('${account}','${pswd}','${type_id}','${username}')`
                conn.query(querystr, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    },
    // 根据user_id获取用户信息
    getUserInfo (id) {
        return new Promise((resolve, rej) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    rej(err)
                    conn.release()
                    return
                }
                let querystr = `select user_id,account,pswd,username,type_id from users where user_id=?`
                let sqlstrParam = [id]
                conn.query(querystr, sqlstrParam, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    },
    // 编辑用户
    editUserInfo (obj) {
        return new Promise((resolve, rej) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    rej(err)
                    conn.release()
                    return
                }
                const { account, pswd, type_id, username, user_id } = obj
                let querystr = `update users set account='${account}',pswd='${pswd}',type_id=${type_id},username='${username}' where user_id=${user_id} `
                conn.query(querystr, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    },
    // 删除用户
    delUser (id) {
        return new Promise((resolve, rej) => {
            mysqlpool.getConnection(function (err, conn) {
                if (err) {
                    rej(err)
                    conn.release()
                    return
                }

                let querystr = `delete from users where user_id=${id} `
                conn.query(querystr, function (err, result) {
                    conn.release()
                    resolve({ err, result })
                })
            })
        }).then(data => data).catch(err => err)
    }
}